import Vue from 'vue'
import Vuex from 'vuex'
import prolist from '/data.json'

Vue.use(Vuex)

export default new Vuex.Store({
    state: sessionStorage.getItem('state') ? (JSON).parse(sessionStorage.getItem('state')) : {
        taglist: [
            {
                name: "主页",
                routeName: "/system/DefaultManual",
                aliveName: "DefaultManual",
                collapse: false,
            },
            {
                name: "图书管理",
                routeName: "/system/BookManage",
                aliveName: "BookManage",
                collapse: true,
            },
            {
                name: "期刊管理",
                routeName: "/system/MagazineManage",
                aliveName: "MagazineManage",
                collapse: true,
            }, {
                name: "借阅管理",
                routeName: "/system/BorrowManage",
                aliveName: "BorrowManage",
                collapse: true,
            }, {
                name: "人事管理",
                routeName: "/system/StaffManage",
                aliveName: "StaffManage",
                collapse: true,
            }, {
                name: "公告",
                routeName: "/system/ProClaim",
                aliveName: "ProClaim",
                collapse: true,
            }, {
                name: "编辑器",
                routeName: "/system/Editor",
                aliveName: "Editor",
                collapse: true,
            }
        ],
        prolist: prolist.proclaim,//从data.JSON导入初始数据
        username: "",
        alivelist: [],
        taglength: 0,
        activeNum: 0,
        activeName: "主页",
    },
    getters: {
        getProclaim: function (state) {
            return function (title) {
                let value = state.prolist.filter(item => item.title === title)
                return value[0]
            }
        },
        getTitleList: state => { //遍历公告返回标题数组
            let TitleList = []
            state.prolist.forEach((item, i, a) => {
                TitleList.push(item.title)
            })
            return TitleList
        },
        checkTitle: state => {
            return function (title) {
                if (state.prolist.findIndex(item => item.title === title) !== -1) {
                    console.log(state.prolist.findIndex(item => item.title === title))
                    return 1
                } else {
                    return 0
                }
            }
        }
    },
    mutations: {
        //标签模块
        open(state, step) {
            state.taglist[step].collapse = false;
        },
        close(state, step) {
            state.taglist[step].collapse = true;
        },
        change(state) {
            state.alivelist = [];
            state.taglist.forEach(
                function (value, index, arr) {
                    if (value.collapse === false) {
                        state.alivelist.push(value.aliveName);
                    }
                })
        },
        changetaglength(state, step) {
            state.taglength = step;
        },
        addMainPage() {
            this.commit('open', 0);
        },
        setActiveByNum(state, step) {
            this.state.activeNum = step;
            this.state.activeName = this.state.taglist[step].name;
        },
        setUserName(state, step) {
            this.state.username = step;
        },
        setActiveByName(state, step) {
            this.state.taglist.forEach(
                (value, index, arr) => {
                    if (value.name === step) {
                        this.state.activeName = value.name;
                        this.state.activeNum = index;
                    }
                })
        },
        //公告模块
        saveNewProclaim(state, step) {
            this.state.prolist.push({
                title: step.title,
                author: step.author,
                date: step.date,
                html: step.html
            })
        },
        deleteProclaim(state, step) {
            let index = this.state.prolist.findIndex(item => item.title === step)
            this.state.prolist.splice(index, 1)
        },
        amendProclaim(state, step) {
            let index = this.state.prolist.findIndex(item => item.title === step)
            this.state.prolist.splice(index, 1, step)
        }
    },
    actions: {},
    modules: {}
})
